<?php
class MyUtil{
	
    public static function getRemoteAddr() {
         if (empty($_SERVER['REMOTE_ADDR']) && empty($_SERVER['HTTP_CLIENTIP'])) {
             return null;
         }
         elseif (empty($_SERVER['HTTP_CLIENTIP'])) {
             return $_SERVER['REMOTE_ADDR'];
         }
         else {
             return $_SERVER['HTTP_CLIENTIP'];
         }
     }
    
    public static function today(){
        return mktime(0,0,0,date('m'),date('d'),date('Y'));
    }

	public static function getCategoryIdAndPageNo($idAndPageNoStr = ''){
		if($idAndPageNoStr === ''){
			return array('cid'=>3,'pageno'=>1);
		}	
		$tmp = explode('_',$idAndPageNoStr);
		$categoryId = intval($tmp[0]);
		$pageNo = count($tmp) === 3 ? intval($tmp[2]) : 1;
		return array('cid'=>$categoryId,'pageno'=>$pageNo);
	}
	
	/**
	 * $url http://www.5show5.com/book/update.php
	 * $host www.5show5.com
	 * $post_data = array('book_id'=>$book_id,'book_name'=>$book_name,'book_pic'=>$book_pic,'sign'=>md5($book_id.'ccs8_cn'))
	 * $cookie X
	 */
	public static function postData($url, $host, $post_data=array(), $cookie='') {
	     $ch = curl_init();
	     curl_setopt($ch, CURLOPT_URL, $url);
	     if(!empty($hostName)) {
	          curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: ' . $host));
	     }
	     curl_setopt($ch, CURLOPT_COOKIE, $cookie);
	     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  FALSE);
	     curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
	     curl_setopt($ch, CURLOPT_POST, 1);
	     curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
	     curl_setopt($ch, CURLOPT_USERAGENT, 'YOUKU.COM BC Test PHP5 Client ver: ' . phpversion());
	     $result = curl_exec($ch);
	     if(false === $result) {
	          $result = null;
	     }
	     curl_close($ch);
	     return $result;
	}
	
}
